import sensor, image, time, gc

#调试用

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.HQVGA)
sensor.skip_frames(time = 200)
sensor.set_auto_gain(False) # 颜色跟踪必须关闭自动增益
sensor.set_auto_whitebal(False) # 颜色跟踪必须关闭白平衡


#定义需要裁剪的ROI
ROI_Shang = (0,60,240,80)
ROI_Xia = (0,80,240,80)

#定义需要识别颜色的ROI
LeftROI_Shang = (25,15,40,60)
MiddleROI_Shang = (100,15,40,60)
RightROI_Shang = (180,14,40,60)
LeftROI_Xia = (35,20,50,60)
MiddleROI_Xia = (100,15,40,40)
RightROI_Xia = (170,20,40,55)

#左 中 右三个ROI的中心点x值与y值
Left_x = 0
Middle_x = 0
Right_x = 0
Left_y = 0
Middle_y = 0
Right_y = 0



def Get_ROI(Rev_Num):
    global Left_x
    global Middle_x
    global Right_x
    global Left_y
    global Middle_y
    global Right_y
    if (Rev_Num == 1):
        #裁剪窗口
        sensor.set_windowing(ROI_Shang)
        #获取左中右三个位置的X坐标和Y坐标
        Left_x = LeftROI_Shang[0] + (LeftROI_Shang[2] / 2)
        Middle_x = MiddleROI_Shang[0] + (MiddleROI_Shang[2] / 2)
        Right_x = RightROI_Shang[0] + (RightROI_Shang[2] / 2)
        Left_y = LeftROI_Shang[1] + (LeftROI_Shang[3] / 2)
        Middle_y = MiddleROI_Shang[1] + (MiddleROI_Shang[3] / 2)
        Right_y = RightROI_Shang[1] + (RightROI_Shang[3] / 2)
        img = sensor.snapshot()
        img.draw_rectangle(LeftROI_Shang)
        img.draw_rectangle(MiddleROI_Shang)
        img.draw_rectangle(RightROI_Shang)
        img.draw_cross(int(Left_x), int(Left_y))
        img.draw_cross(int(Middle_x), int(Middle_y))
        img.draw_cross(int(Right_x), int(Right_y))
    elif (Rev_Num == 2):
        #裁剪窗口
        #sensor.set_windowing(ROI_Xia)
        #获取左中右三个位置的X坐标和Y坐标
        Left_x = LeftROI_Xia[0] + (LeftROI_Xia[2] / 2)
        Middle_x = MiddleROI_Xia[0] + (MiddleROI_Xia[2] / 2)
        Right_x = RightROI_Xia[0] + (RightROI_Xia[2] / 2)
        Left_y = LeftROI_Xia[1] + (LeftROI_Xia[3] / 2)
        Middle_y = MiddleROI_Xia[1] + (MiddleROI_Xia[3] / 2)
        Right_y = RightROI_Xia[1] + (RightROI_Xia[3] / 2)
        img = sensor.snapshot()
        img.draw_rectangle(LeftROI_Xia)
        img.draw_rectangle(MiddleROI_Xia)
        img.draw_rectangle(RightROI_Xia)
        img.draw_cross(int(Left_x), int(Left_y))
        img.draw_cross(int(Middle_x), int(Middle_y))
        img.draw_cross(int(Right_x), int(Right_y))



while(True):
    Get_ROI(1)
    gc.collect()

